//
//  OMAdviseSubmitViewController.m
//  OMENGO
//
//  Created by 梁尚嘉 on 2017/4/20.
//  Copyright © 2017年 Kamfat. All rights reserved.
//

#import "OMAdviseSubmitViewController.h"
#import "KTInputViewTableCell.h"
#import "NSString+Regex.h"
#import "KTEleInputTableCell.h"

@interface OMAdviseSubmitViewController ()
@property (nonatomic, strong) KTInputViewTableCell *adviseInputCell;
@property (nonatomic, strong) KTEleInputTableCell *phoneCell;
@end

@implementation OMAdviseSubmitViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupUI];
    [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.navigationBar.mas_bottom);
        make.bottom.equalTo(@0);
        make.left.equalTo(@(SM_SIZE(30)));
        make.right.equalTo(@(SM_SIZE(-30)));
    }];
}

- (void)setupUI
{
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"提交"
                                                                             style:UIBarButtonItemStyleDone
                                                                            target:nil
                                                                            action:nil];
    @weakify(self);
    self.navigationItem.rightBarButtonItem.rac_command = [[RACCommand alloc]initWithSignalBlock:^RACSignal *(id input)
                                                          {
                                                              @strongify(self);
                                                              [self feedBack];
                                                              return [RACSignal empty];
                                                          }];

    _adviseInputCell = [KTInputViewTableCell tableViewCellWithTableView:self.tableView];
    _adviseInputCell.placeholder = @"请输入您的反馈意见吧，我们将努力改进！";
    _adviseInputCell.placeholderColor = HEXCOLOR(@"#B9B8B9");
    _adviseInputCell.layer.cornerRadius = SM_SIZE(7);
    _adviseInputCell.layer.borderColor = SM_naviShadowColor.CGColor;
    _adviseInputCell.layer.borderWidth = 0.5f;
    _adviseInputCell.clipsToBounds = true;
    _adviseInputCell.maxLength = 200;
    [self.adviseInputCell.textView.rac_textSignal subscribeNext:^(id x) {
        @strongify(self);
        self.navigationItem.rightBarButtonItem.enabled = self.adviseInputCell.textView.text.length;
    }];

    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SM_SCREEN_WIDTH, SM_SIZE(30))];

    self.phoneCell = [KTEleInputTableCell new];
    self.phoneCell.elementLabel.text = @"手机号码";
    self.phoneCell.inputTF.keyboardType = UIKeyboardTypeNumberPad;
    self.phoneCell.inputTF.placeholder = @"填写手机号码，便于我们与您联系";
}


#pragma mark - tableview delegate && datasource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section) {
        return self.phoneCell;
    }
    return self.adviseInputCell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section) {
        return [KTEleInputTableCell cellHeight];
    }
    return SM_SIZE(500);
}


- (void)feedBack
{
    if (self.phoneCell.inputTF.text.length&&![self.phoneCell.inputTF.text isMobile]) {
        [OMHUD HUDInfo:@"请输入正确的手机号码!"];
        return;
    }
    [self startAnimating:YES];
    [KTHttpUtils POST:@"member/feedback.jhtml"
           parameters:@{@"suggestion":self.adviseInputCell.textView.text,@"mobile":self.phoneCell.inputTF.text?:@""}
             progress:nil
           completion:^(id responseObject, BOOL isSuccess, NSString *message) {
               [self startAnimating:NO];
               if (isSuccess) {
                   [self.view endEditing:YES];
                   [KTAlertView alertWithMessage:@"我们已经收到你的意见，感谢参与!" Title:@"提交成功！" ButtonTitles:nil withClickBlock:nil];
                   NaviPopAnimated(true);
               }else {
                   [OMHUD HUDInfo:@"提交失败!"];
               }
           }];
}
@end
